﻿using HZYProject.Core.Repositories;
using HZYProject.Domain;
using HZYProject.Service;
using HZYProject.Service.CollegeServices;
using HZYProject.Web.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace HZYProject.Web.Controllers
{
    /// <summary>
    /// 院校管理
    /// </summary>
    [Route("College")]
    public class CollegeController : Controller
    {
        private readonly IRepository<Colleges, Guid> _repository;
        private readonly ICollegeService _collegeService;

        public CollegeController(IRepository<Colleges, Guid> repository, ICollegeService collegeService)
        {
            _repository = repository;
            _collegeService = collegeService;
        }

        #region 首页分页查询
        [HttpGet("")]
        public IActionResult Index()
        {
            return View();
        }
        [HttpPost("")]
        public async Task<IActionResult> Index(UICollege c)
        {
            try
            {
                GetCollegeInput input = new GetCollegeInput { ID = c.CollegesID, Name = c.Name };
                var data = await _collegeService.GetPaginatedResult(input);
                return Json(new { code = 0, msg = "", data = data.Data, count = data.TotalCount });
            }
            catch (Exception ex)
            {
                return Json(new { code = 1, msg = "查询出错:" + ex.Message, data = new List<UICollege>(), count = 0 });
            }
        }
        #endregion

        #region 添加
        [HttpGet("add")]
        public IActionResult Add()
        {
            return View();
        }
        [HttpPost("add")]
        public IActionResult Add(UICollege col)
        {
            try
            {
                Colleges c = new Colleges();
                c.CollegesID = Guid.NewGuid();
                c.Name = col.Name;
                _repository.Insert(c);
                return Json(new { success = true, msg = "添加成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion

        #region 修改
        [HttpGet("edit")]
        public IActionResult Edit(Guid id)
        {
            var obj = _repository.FirstOrDefault(x => x.CollegesID == id);
            return View(obj);
        }
        [HttpPost("edit")]
        public IActionResult Edit(UICollege col)
        {
            try
            {
                if (ModelState.IsValid)//obj在形成对象时，是否全部符合注解中的规定
                {
                    var obj = _repository.FirstOrDefault(x => x.CollegesID == col.CollegesID);
                    obj.Name = col.Name;
                    _repository.Update(obj);
                    return Json(new { success = true, msg = "修改成功" });
                }
                else
                {
                    System.Text.StringBuilder sb = new System.Text.StringBuilder();
                    int i = 1;
                    foreach (var v in ModelState.Values)
                    {
                        foreach (var p in v.Errors)
                        {
                            sb.AppendFormat("{0}-{1};", i++, p.ErrorMessage);
                        }
                    }
                    return Json(new { success = false, msg = "服务端验证未通过," + sb });
                }
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }

        }
        #endregion

        #region 删除
        [HttpPost("remove/{id}")]
        public IActionResult Del(Guid id)
        {
            try
            {
                Colleges c = new Colleges { CollegesID = id };
                //var obj = _repository.FirstOrDefault(x => x.CollegesID == id);
                _repository.Delete(c);
                return Json(new { success = true, msg = "删除成功" });
            }
            catch (Exception ex)
            {
                return Json(new { success = false, msg = ex.Message });
            }
        }
        #endregion
    }
}
